#include "hbr.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"
#include "cnfield.h"

using namespace std;
using namespace opsrc::stdf;

Hbr::Hbr()
{
}

Hbr::Hbr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex,  endian e, char* buf)
    :RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e) {
    if (buf!=0)
        readFields(buf);
}

Hbr::~Hbr() {
}


const StdfField<int> Hbr::getHeadNum() const {
    return head_num;
}

void Hbr::setHeadNum(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Hbr::getSiteNum() const {
    return site_num;
}

void Hbr::setSiteNum(StdfField<int> sitenum) {
    site_num = sitenum;
}

const StdfField<int> Hbr::getHbinNum() const {
    return hbin_num;
}

void Hbr::setHbinNum(StdfField<int> hbinnum) {
    hbin_num = hbinnum;
}

const StdfField<long> Hbr::getHbinCount() const {
    return hbin_cnt;
}

void Hbr::setHbinCount(StdfField<long> hbincnt) {
    hbin_cnt = hbincnt;
}

const StdfField<string> Hbr::getHbinPassFail() const {
    return hbin_pf;
}

void Hbr::setHbinPassFail(StdfField<string> hbinpf) {
    hbin_pf = hbinpf;
}

const StdfField<string> Hbr::getHbinName() const {
    return hbin_nam;
}

void Hbr::setHbinName(StdfField<string> hbinnam) {
    hbin_nam = hbinnam;
}

void Hbr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= HBR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read head number , 1 byte
    NmField<unsigned int, 1> headnum(buf+curpos, en);
    head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
    curpos += head_num.getTotalByteCount();

    // Read site num, 1 byte
    if (curpos < reclen -1){
        NmField<unsigned int, 1> sitenum(buf+curpos, en);
        site_num.set(sitenum.getData(), curpos, sitenum.getByteCount(), sitenum.getTotalByteCount());
        curpos += site_num.getTotalByteCount();
    }

    // Read hbin num 2 byte
    if (curpos < reclen -1){
        NmField<unsigned int, 2> hbinnum(buf+curpos, en);
        hbin_num.set(hbinnum.getData(), curpos, hbinnum.getByteCount(), hbinnum.getTotalByteCount());
        curpos += hbin_num.getTotalByteCount();
    }

    // Read hbin count
    if (curpos < reclen -1){
        NmField<unsigned int, 4> hbincnt(buf+curpos, en);
        hbin_cnt.set(hbincnt.getData(), curpos, hbincnt.getByteCount(), hbincnt.getTotalByteCount());
        curpos += hbin_cnt.getTotalByteCount();
    }

    // read hbin pass fail
    if (curpos < reclen-1) {
        CnField<string,1> hbinpf(buf+curpos, en);
        hbin_pf.set(hbinpf.getData(), curpos, hbinpf.getByteCount(), hbinpf.getTotalByteCount());
        curpos += hbin_pf.getTotalByteCount();
    }

    // read hbin name
    if (curpos < reclen-1) {
        CnField<string> hbinnam(buf+curpos, en);
        hbin_nam.set(hbinnam.getData(), curpos, hbinnam.getByteCount(), hbinnam.getTotalByteCount());
    }
}
